<template>
    <!-- <span> -->
    <el-table-column v-if="label != '主键ID'" :formatter="formatter" :type="type" :fixed="fixed" :prop="prop" 
        :sortable="sortable" :show-overflow-tooltip="showOverflowTooltip"
        :align="align"
        :label="label" :width="width" ></el-table-column>
    <!-- </span> -->
</template>

<script>
module.exports = {
    data: function() {
        return {
            fieldMap: null
        }
    },
    props: {
        mapName: String,
        sortable: String,
        type: String,
        fixed: String,
        prop: String,
        label: String,
        align: String,
        width: String,
        showOverflowTooltip: Boolean,
        // 值映射
        map: Object
    },
    created: function() {
        var $table = this.$parent.$parent;
        if($table != null) {
            this.fieldMap = $table.map;
        }
        // console.log('parent idName', this.$parent.idName);
    },
    methods: {
        formatterList: function(row, column, cellValue, index) {
            var values = cellValue.split(",");
            var formatValues = [];
            for(var i in values) {
                var value = values[i];
                var formatValue = this.formatter(row, column, value, index);
                formatValues.push(formatValue);
            }
            return formatValues.join(",");
        },
        getFieldMap: function() {
            if(this.fieldMap == null) {
                return null;
            }
            var fieldMap = this.fieldMap[this.prop];
            if(fieldMap == null) {
                fieldMap = this.map;
            }
            return fieldMap;
        },
        formatter: function(row, column, cellValue, index) {
            // if(this.fieldMap == null) {
            //     return cellValue;
            // } 
            if(cellValue == null) {
                return null;
            }
            
            if( Util.isString(cellValue) && cellValue.indexOf(",") != -1) {
                return this.formatterList(row, column, cellValue, index);
            }
            var fieldMap = this.getFieldMap();
            if(fieldMap == null) {
                return cellValue;
            }
            // 存在映射的则返回映射
            if(fieldMap[cellValue] != null) {
                return fieldMap[cellValue];
            } else {
                // 不存在则返回默认值
                return cellValue;
            }
        }
    }
}
</script>